﻿import idv.ben.gc4flash.chart.*;
import idv.ben.gc4flash.dataLine.*;
import idv.ben.gc4flash.encode.*;
import idv.ben.gc4flash.fill.*;
import idv.ben.gc4flash.style.*;
import idv.ben.gc4flash.util.*;
/**
* 折線圖。<br>
* 參考 Google Chart API：<a href="http://code.google.com/apis/chart/#line_charts" target="_blank">http://code.google.com/apis/chart/#line_charts</a><br>
*/
class idv.ben.gc4flash.chart.LineChart extends AbstractChart {
	private var gridLinesStyle:GridLinesStyle;
	/**
	* 基本折線圖<br>
	* <img src="http://chart.apis.google.com/chart?cht=lc&chs=200x125&chd=s:fooZaroo"><br>
	* cht=lc
	*/
	public static var CHART_TYPE_LineChart_Simple:String = "lc";
	/**
	* 可個別設定 x, y 值的折線圖<br>
	* <img src="http://chart.apis.google.com/chart?cht=lxy&chs=200x125&chd=t:0,30,60,70,90,95,100|20,30,40,50,60,70,80|10,30,40,45,52|100,90,40,20,10|-1|5,33,50,55,7&chco=3072F3,ff0000,00aaaa&chls=2,4,1&chm=s,FF0000,0,-1,5|s,0000ff,1,-1,5|s,00aa00,2,-1,5"><br>
	* cht=lxy
	*/
	public static var CHART_TYPE_LineChart_XY:String = "lxy";
	/**
	* 建構子
	* @param w 圖檔寬度
	* @param h 圖檔高度
	* @param typ 圖形種類，包括：<br>
	* {@link #CHART_TYPE_LineChart_Simple} = "lc"<br>
	* {@link #CHART_TYPE_LineChart_XY} = "lxy"<br>
	*/
	function LineChart(w:Number, h:Number, typ:String) {
		super(w, h);
		if(typ==CHART_TYPE_LineChart_Simple || typ==CHART_TYPE_LineChart_XY){
			this.typ = typ
		}else{
			throw new Error("chart type error!!");
		}
		allowParams = Const.CHT_PARAM_Line_chart;
	}
	/**
	* 新增一條折線資料
	* @param values 單一折線資料的數值陣列，[n1:Number, n2:Number, ..., nN:Number]
	* @param legend 圖示說明<br>
	* <img src="http://chart.apis.google.com/chart?&cht=lc&chd=t:0,30,60,70,90,95,100|20,30,40,50,60,70,80|10,30,40,45,52&chco=ff0000,00ff00,0000ff&chs=250x150&chdl=First|Second|Third"><br>
	* chdl=First|Second|Third&chco=ff0000,00ff00,0000ff<br>
	* @param lineStyle {@link idv.ben.gc4flash.dataLine.LineStyle}
	*/
	public function addLine(values:Array, legend:String, lineStyle:LineStyle):Void {
		if (typ == CHART_TYPE_LineChart_Simple) {
			var ds:ChartDataSet = new ChartDataSet();
			for(var i:Number=0; i<values.length; i++){
				ds.addValue(values[i]);
			}
			var dataLine:DataLine = new LineChartDataLine([ds], legend, lineStyle);
			lines.push({line:dataLine});
		}
	}
	/**
	* 新增一條折線資料
	* @param values 單一折線資料的數值陣列，允許個別設定每個資料的 x, y 值，[{x:Number, y:Number}, {x:Number, y:Number}, ..., {x:Number, y:Number}]
	* @param legend 圖示說明<br>
	* <img src="http://chart.apis.google.com/chart?&cht=lc&chd=t:0,30,60,70,90,95,100|20,30,40,50,60,70,80|10,30,40,45,52&chco=ff0000,00ff00,0000ff&chs=250x150&chdl=First|Second|Third"><br>
	* chdl=First|Second|Third&chco=ff0000,00ff00,0000ff<br>
	* @param lineStyle {@link idv.ben.gc4flash.dataLine.LineStyle}
	*/
	public function addLineXY(values:Array, legend:String, lineStyle:LineStyle):Void {
		if (typ == CHART_TYPE_LineChart_XY) {
			var ds_x:ChartDataSet = new ChartDataSet();
			var ds_y:ChartDataSet = new ChartDataSet();
			for(var i:Number=0; i<values.length; i++){
				ds_x.addValue(values[i].x);
				ds_y.addValue(values[i].y);
			}
			var dataLine:DataLine = new LineChartDataLine([ds_x, ds_y], legend, lineStyle);
			lines.push({line:dataLine});
		}
	}
	/**
	* 設定格子樣式
	* @param gridLinesStyle {@link idv.ben.gc4flash.dataLine.GridLinesStyle}
	**/
	public function setGridLinesStyle(gridLinesStyle:GridLinesStyle):Void{
		this.gridLinesStyle = gridLinesStyle;
	}
	/**
	* 取得格子樣式所組成的 URL 參數字串
	* @return URL 參數字串
	**/
	public function getGridLinesStyleChartData():String{
		if(gridLinesStyle!=null){
			return "&chg=" + gridLinesStyle.toChartData();
		}else{
			return "";
		}
	}
}
